import { OnInit, Component } from "@angular/core";
import { IonicPage, Nav, MenuController } from 'ionic-angular';
import { FormGroup, FormBuilder, Validators } from "@angular/forms";
import { URLSearchParams } from '@angular/http';
import { LoginPage } from '../login/login';

import { HelperService } from "../../providers/services.helper";
import { ApiService } from "../../providers/services.api";

/**
 * Generated class for the ActivatePage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-activate',
  templateUrl: 'activate.html',
})
export class ActivatePage {
  authForm: FormGroup;

  constructor(private menu: MenuController, public navCtrl: Nav, public fb: FormBuilder, public helperService: HelperService, public api: ApiService) {
    this.authForm = fb.group({
      'username': [null, Validators.compose([Validators.required, Validators.minLength(1), Validators.maxLength(20)])],
      'password': [null, Validators.compose([Validators.required, Validators.minLength(5)])]
    })
  }

  submitForm(value: any): void {
    console.log('Activate Form submited!')
    console.log(value);
    let urlParams = new URLSearchParams();
    urlParams.append('app_name', value.username);
    urlParams.append('app_id', value.password);
    console.log('urlParams', urlParams);
    this.api.activation(urlParams).then(data => {
      console.log('activation success:' + data);
      this.navCtrl.setRoot(LoginPage);

    }, (error) => {
      console.log('activation error:' + error);
      this.helperService.showToast(error);
    });
  }

  ionViewDidEnter() {
    this.menu.swipeEnable(false, 'left-menu');
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ActivatePage');
  }

  ionViewWillLeave() {
    console.log("ionViewWillLeave ActivatePage");
  }
}
